package net.caixiaomi.info.ui.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;

import com.baidu.mobstat.StatService;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.CommonFragmentPageAdapter;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.CommonTab;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import icepick.State;


/**
 * 消息中心
 */
public class MessageCenterActivity extends BaseActivity {

    @State
    int mPosition;

    @BindView(R.id.tab)
    TabLayout mTabLayout;
    @BindView(R.id.pager)
    ViewPager mViewPager;
    private CommonFragmentPageAdapter mAdapter;
    private TabLayout.TabLayoutOnPageChangeListener mChangeListener = new TabLayout.TabLayoutOnPageChangeListener(mTabLayout) {

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            AppHelper.sendStatistics(position == 0 ? "xxzxtongzhi" : "xxzxxiaoxi");
            mPosition = position;
        }
    };

    @Override
    protected int layout() {
        return R.layout.act_coupons;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] tabArray = getResources().getStringArray(R.array.TAB_MESSAGE_ARRAY);
        List<Fragment> data = new ArrayList<>();
        List<CommonTab> titles = new ArrayList<>();
        for (int index = 0; index < tabArray.length; index++) {
            if (index == 0) {
                NoticeFragment fragment = new NoticeFragment();
                CommonTab tab = new CommonTab(index, tabArray[index]);
                titles.add(tab);
                fragment.setType(tab.getType());
                data.add(fragment);
            } else {
                MessageFragment fragment = new MessageFragment();
                CommonTab tab = new CommonTab(index, tabArray[index]);
                titles.add(tab);
                fragment.setType(tab.getType());
                data.add(fragment);
            }
        }

        mAdapter = new CommonFragmentPageAdapter(getSupportFragmentManager(), titles, data);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.layout_divider_vertical));
        mViewPager.addOnPageChangeListener(mChangeListener);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setCurrentItem(mPosition);
//        mTabLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                AppHelper.setIndicator(mTabLayout);
//            }
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "xiaoxizhongxin-view");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "xiaoxizhongxin-view");
    }
    @Override
    public void onBackPressed() {
        AppHelper.sendStatistics("xxzxfanhui");
        super.onBackPressed();
    }
}
